<?php
include_once('_PATH.php');
require_once($_CLASSPATH . '/ErrorCodes.class.php');

require_once($_CLASSPATH . '/model/Mood.class.php');
require_once($_CLASSPATH . '/service/MoodService.class.php');
require_once($_CLASSPATH . '/service/UserService.class.php');

class AppFrontController {
	function updateMood($_uid, $_moodType, $_moodRate, $_moodNote) {
		$moodService = new MoodService();
		$moodService->setMood( $_uid , $_moodType, $_moodRate, $_moodNote) ;
	}
}

class FacebookFrontController {
	private $app;

	function __construct(  ) {
		$this->app = new AppFrontController();
	}
	
	function registerSocialUser($_socialNet, $_socialUid) {
		$userService = new UserService();
		return $userService->createSocialUser( $_socialNet, $_socialUid );
	}
	
	function updateMood() {
		$userService = new UserService();
		$uid = $userService->getUserBySociaId( $FACEBOOKID, $_REQUEST['fb_sig_user'] );
		$this->app->updateMood( $uid, $_REQUEST['moodType'], ( isset( $_REQUEST['moodRate'] ) ? $_REQUEST['moodRate'] : 1 ) , $_REQUEST['moodNote'] );
	}
	
	function & getMood() {
		$moodService = new MoodService();
		$uid = $_REQUEST['uid'];
		$mood = & $moodService->getCurrentMood( $uid );
		return $mood;
	}
	
	function execute() {
		$FACEBOOKID = 1;

		switch ( @ $_REQUEST['act'] ) {
			case 'postLogin':
				$uid = $this->registerSocialUser( $FACEBOOKID , $_REQUEST['fb_sig_user'] );
				break;
			case 'setMood':
				$this->updateMood();
				break;
			case 'getMood':
				$mood = & $this->getMood();
				break;
		}
	}
}

$actionControl = new FacebookFrontController();
$actionControl->execute();

/*
$host = $_SERVER['HTTP_HOST'];
$context = dirname( $_SERVER['PHP_SELF'] ) ;

$qryStr = '?';
if ( isset( $_REQUEST['offl'] ) )
	$qryStr .= 'offl=1';

$addr = 'http://' . $host . $context . '/index.php' . $qryStr;
header( 'Location: ' . $addr ) ;
*/
?>